package com.atguigu.lease.web.admin.custom.interceptors;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        //1.获取request中的请求头的token
        String  accessToken=request.getHeader("access_token");
        //2.检查token是否为null
        if (ObjectUtils.isEmpty(accessToken)){
            //没有token，没有登录
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        //3.检查token是否过期和有效
        JwtUtil.parseToken(accessToken);
        //true放行 | false拦截
        return true;
    }
}
